// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Constraint/RSPLINEParser.h"

#include "gtest/gtest.h"

#include "DataStructure/Common/Constant.h"
#include "DataStructure/Input/Constraint/RSPLINEData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/IRSPLINEService.h"

#include "Import/BDFImport/BDFParserBase.h"

using SG::DataStructure::Common::TOLERANCE;
using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;
class RSPLINEParserTest : public ::testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        DB_SERVICEFACTORY->ClearDatabase ();
    }
};

TEST_F (RSPLINEParserTest, ParseEntry1)
{
    // dbManager->Initialize ();

    std::vector<std::string> Entry = { "RSPLINE* 8135           .100000001490116 12              2420",
                                       "*        123456          2445            123456          2462",
                                       "         123456  2479    123456  2490    23456   2501    23456   2516",
                                       "         23456   36" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseRSPLINE (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::RSPLINEData data;

    DB_SERVICEFACTORY->get<IRSPLINEService> ()->FindByIndex (0, data);

    ASSERT_TRUE (data.m_id == 8135);
    ASSERT_NEAR (data.m_slender, 0.100000001490116, TOLERANCE);
    ASSERT_TRUE (data.m_g1 == 12);
    ASSERT_TRUE (data.m_items.size () == 8);
    ASSERT_TRUE (data.m_items[0].GetDofs ().size () == 6);
    ASSERT_TRUE (data.m_items[1].GetDofs ().size () == 6);
    ASSERT_TRUE (data.m_items[2].GetDofs ().size () == 6);
    ASSERT_TRUE (data.m_items[3].GetDofs ().size () == 6);
    ASSERT_TRUE (data.m_items[4].GetDofs ().size () == 5);
    ASSERT_TRUE (data.m_items[5].GetDofs ().size () == 5);
    ASSERT_TRUE (data.m_items[6].GetDofs ().size () == 5);
    ASSERT_TRUE (data.m_items[7].GetDofs ().size ()
                 == 0);  // A blank field for Ci indicates that all six degrees-of-freedom at Gi are independent

    ASSERT_TRUE (data.m_items[0].m_nodeId == 2420);
    ASSERT_TRUE (data.m_items[1].m_nodeId == 2445);
    ASSERT_TRUE (data.m_items[2].m_nodeId == 2462);
    ASSERT_TRUE (data.m_items[3].m_nodeId == 2479);
    ASSERT_TRUE (data.m_items[4].m_nodeId == 2490);
    ASSERT_TRUE (data.m_items[5].m_nodeId == 2501);
    ASSERT_TRUE (data.m_items[6].m_nodeId == 2516);
    ASSERT_TRUE (data.m_items[7].m_nodeId == 36);
}

TEST_F (RSPLINEParserTest, ParseEntry2)
{
    // dbManager->Initialize ();

    std::vector<std::string> Entry = { "RSPLINE        2               2       5     123      12     123       4" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseRSPLINE (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::RSPLINEData data;

    DB_SERVICEFACTORY->get<IRSPLINEService> ()->FindById (2, data);

    ASSERT_TRUE (data.m_id == 2);
    ASSERT_NEAR (data.m_slender, 0.1, TOLERANCE);
    ASSERT_TRUE (data.m_g1 == 2);
    ASSERT_TRUE (data.m_items.size () == 3);
    ASSERT_TRUE (data.m_items[0].GetDofs ().size () == 3);
    ASSERT_TRUE (data.m_items[1].GetDofs ().size () == 3);
    ASSERT_TRUE (data.m_items[2].GetDofs ().size () == 0);

    ASSERT_TRUE (data.m_items[0].m_nodeId == 5);
    ASSERT_TRUE (data.m_items[1].m_nodeId == 12);
    ASSERT_TRUE (data.m_items[2].m_nodeId == 4);
}
